﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sqengine.Engine.Network
{


    [Serializable]
    public enum LobbyType
    {
        SearchForPlay = 1,
        KilledBy = 2,
        Kill = 3,
        MsgTo = 4,
    }

    [Serializable]
    public class LobbyPacket
    {
        private string _source;
        private string _location;
        private LobbyType _type;
        private string _message;
        private List<string> _otherparams;

        //{@dest} [sqen]squid#location#type#message
        public string Source { get { return _source; } set { _source = value; } }
        public string Location { get { return _location; } set { _location = value; } }
        public LobbyType Type { get { return _type; } set { _type = value; } }
        public string Message { get { return _message; } set { _message = value; } }
        public List<string> OtherParams { get { return _otherparams; } set { _otherparams = value; } }

    }

    public class LobbyProtocol
    {
        private const string LOBBY_PREFIX = "[sqen]";

        public static bool VerifyUrl(string lpURL)
        {
            if (lpURL.Contains(LOBBY_PREFIX)) { return true; } else { return false; }
        }

        public static LobbyPacket ParseUrl(string lpurl)
        {
            if (VerifyUrl(lpurl))
            {

                lpurl = lpurl.Replace(LOBBY_PREFIX, string.Empty);

                if (lpurl.Split('#').Length >= 3)
                {
                    //{@dest} [sqen]squid#location#type#message
                    LobbyPacket lob_pck = new LobbyPacket();
                    string[] lp_parsed = lpurl.Split('#');
                    lob_pck.Source = lp_parsed[0];
                    lob_pck.Location = lp_parsed[1];
                    lob_pck.Type = (LobbyType)((int.Parse(lp_parsed[2])));
                    lob_pck.Message = lp_parsed[3];

                    if (lp_parsed.Length > 3)
                    {
                        for (int i = 4; i < lp_parsed.Length; i++)
                        { lob_pck.OtherParams.Add(lp_parsed[i]); }
                    }

                    return lob_pck;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        public static string ParsePacket(LobbyPacket pckd)
        {
            if (pckd != null)
            {
                //[sqen]squid#location#type#message
                string lp_uri = string.Format("{0}{1}#{2}#{3}#{4}", LOBBY_PREFIX, pckd.Source, pckd.Location, (int)(pckd.Type), pckd.Message);

                if (pckd.OtherParams != null)
                {
                    foreach (string item in pckd.OtherParams)
                    {
                        lp_uri += string.Format("#{0}", item);
                    }
                }

                return lp_uri;

            }
            else
            {
                return string.Empty;

            }

        }

        public static string ParsePacket(string Source, string Location, LobbyType Type, string Message, List<string> OtherParams)
        {
            LobbyPacket pck = new LobbyPacket();
            pck.Source = Source;
            pck.Location = Location;
            pck.Type = Type;
            pck.Message = Message;
            pck.OtherParams = OtherParams;

            return ParsePacket(pck);
        }

    }
}
